1 module hunt.cache.ucache;
2 
3 import hunt.cache.memory;
4 import hunt.cache.memcached;
5 import hunt.cache.redis;
6 import hunt.cache.rocksdb;
7 import hunt.cache.cache;
8 import hunt.cache.nullable;
9 
10 import std.conv;
11 import std.stdio;
12 import std.traits;
13 
14 class UCache
15 {
16 	//get
17 	mixin(genfunc("V get(V)(string key )" , "get!V(key);"));
18 	//get_ex
19 	mixin(genfunc("Nullable!V get_ex(V)(string key )" , "get_ex!V(key);"));
20 	//getall
21 	mixin(genfunc("Nullable!V[string] getall(V)(string[] keys)" , "getall!V(keys);"));
22 	mixin(genfunc("string get(string key )" , "get!string(key);"));
23 	//get_ex
24 	mixin(genfunc("Nullable!string get_ex(string key )" , "get_ex!string(key);"));
25 	//getall
26 	mixin(genfunc("Nullable!string[string] getall(string[] keys)" , "getall!string(keys);"));
27 
28 	//containsKey
29 	mixin(genfunc("bool	containsKey(string key)" , "containsKey(key);"));
30 	//put
31 	mixin(genfunc("void put(V)(string key ,  V v , uint expired = 0)" , "put!V(key , v , expired);"));
32 	//putifabsent
33 	mixin(genfunc("bool	putifAbsent(V)(string key ,  V v)" , "putifAbsent!V(key , v);"));
34 	//putAll
35 	mixin(genfunc("void	putAll(V)( V[string] maps , uint expired = 0)" , "putAll!V(maps , expired);"));
36 	//remove
37 	mixin(genfunc("bool	remove(string key)" , "remove(key);"));
38 	//removeAll
39 	mixin(genfunc("void	removeAll(string[] keys)" , "removeAll(keys);"));
40 	//clear
41 	mixin(genfunc("void clear()" , "clear();"));
42 
43 
44 	static UCache CreateUCache(string driverName = "memory"  , 
45 		string args = ""  , bool enableL2  = false )
46 	{
47 		switch(driverName)
48 		{
49 			case "memory":
50 				return new UCache(new Cache!MemoryCache(args , enableL2));
51 				version(SUPPORT_REDIS)
52 				{
53 					case "redis":
54 					return new UCache(new Cache!RedisCache(args , enableL2));
55 				}
56 				version(SUPPORT_MEMCACHED)
57 				{
58 					case "memcached":
59 					return new UCache(new Cache!MemcachedCache(args , enableL2));
60 				}
61 				version(SUPPORT_ROCKSDB)
62 				{
63 					case "rocksdb":
64 					return new UCache(new Cache!RocksdbCache(args , enableL2));
65 				}
66 			default:
67 				return new UCache(new Cache!MemoryCache(args , enableL2));
68 		}
69 		
70 		
71 	}
72 
73 private:
74 
75 	static string genfunc(string callorigin , string callfunc)
76 	{
77 		string f = callorigin ~ " { if(_memory !is null) return  _memory." ~ callfunc ;
78         
79 		version(SUPPORT_REDIS)
80 		{
81 			f ~= " if(_redis !is null) return  _redis." ~ callfunc;
82 		}
83 
84 		version(SUPPORT_MEMCACHED)
85 		{
86 			f ~= "if( _memcahed !is null) return _memcahed." ~ callfunc;
87 		}
88 
89 		version(SUPPORT_ROCKSDB)
90 		{ 
91 			f ~= "if(_rocksdb !is null) return _rocksdb." ~ callfunc ;
92 		}
93         
94 		f ~= "assert(0);}";
95 		return f;	
96 	}
97 
98 	this(Object obj)
99 	{
100 		string className = to!string(typeid(obj));
101 		if(className == to!string(typeid(Cache!MemoryCache)))
102 		{
103 			_memory = cast(Cache!MemoryCache)obj;
104 			return;
105 		}
106 		version(SUPPORT_REDIS)
107 		{
108 			if(className == to!string(typeid(Cache!RedisCache)))
109 			{
110 				_redis = cast(Cache!RedisCache)obj;
111 				return;
112 			}
113 		}
114 		
115 		version(SUPPORT_MEMCACHED)
116 		{ 
117 			if(className == to!string(typeid(Cache!MemcachedCache)))
118 			{	_memcahed = cast(Cache!MemcachedCache)(obj);
119 				return;
120 			}
121 		}
122 		
123 		version(SUPPORT_ROCKSDB){
124 			if(className == to!string(typeid(Cache!RocksdbCache)))
125 			{	
126 				_rocksdb = cast(Cache!RocksdbCache)(obj);
127 				return;
128 			}
129 		}
130 		
131 		assert(0);
132 	}
133 
134 	Cache!MemoryCache			_memory = null;
135 	version(SUPPORT_REDIS)
136 	Cache!RedisCache 			_redis = null;
137 	version(SUPPORT_MEMCACHED)
138 	Cache!MemcachedCache		_memcahed = null;
139 	version(SUPPORT_ROCKSDB)
140 	Cache!RocksdbCache			_rocksdb = null;
141 };